import asyncio
import os

import discord
import unittest
from unittest.mock import AsyncMock, patch
from metagpt.schema import Message

import discord




async def send_discord_msg(channel_id: int, msg: str, token: str):
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)
    async with client:
        await client.login(token)
        channel = await client.fetch_channel(channel_id)
        await channel.send(msg)



async def discord_callback(msg: Message):
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)
    token = os.environ["DISCORD_TOKEN"]
    channel_id = int(os.environ["DISCORD_CHANNEL_ID"])
    async with client:
        await client.login(token)
        channel = await client.fetch_channel(channel_id)
        lines = []
        for i in msg.content.splitlines():
            if i.startswith(("# ", "## ", "### ")):
                if lines:
                    await channel.send("\n".join(lines))
                    lines = []
            lines.append(i)

        if lines:
            await channel.send("\n".join(lines))








if __name__ == '__main__':
    send_discord_msg(1234804498292281399, 'test message', 'MTIzNzU5MTY4MDE2MTU1MDMzNg.Gh4qzl.dY7uz4efPbtNojU-4tplaxqBJOPni--fYOkGuc')